
//
//  OCCHTTPManager.m
//  occ
//
//  Created by plocc on 14-4-18.
//  Copyright (c) 2014年 RS. All rights reserved.
//

#import "BaseHTTPManager.h"

#define  HTTP_DEFAULT_URL @""

@interface BaseHTTPManager ()
@property (strong, nonatomic) AFHTTPRequestOperationManager *manager;
@end

@implementation BaseHTTPManager

+ (instancetype)sharedManager
{
    static BaseHTTPManager *instance = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[BaseHTTPManager alloc] initTaskPool];
    });
    
    return instance;
}

#pragma mark -
#pragma mark Init Methods
+(BaseResponse*)getBaseResponse:(BaseResponse*)rsp
{
    if (rsp==nil) {
        rsp=[[BaseResponse alloc]init];
        rsp.errorCode=1000;
        rsp.errorMsg=@"服务器异常";
    }
    
    return rsp;
}

- (id)initTaskPool
{
    self = [super init];
    if (self)
    {
        //任务序列缓存
        _manager = [AFHTTPRequestOperationManager manager];
        _manager.operationQueue.maxConcurrentOperationCount=10;
        _manager.requestSerializer.timeoutInterval=15.0;
        //[_manager.requestSerializer setValue:@"application/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        [_manager.requestSerializer setValue:[[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey] forHTTPHeaderField:@"version"];
        [_manager.requestSerializer setValue:@"app" forHTTPHeaderField:@"type"];
        [_manager.requestSerializer setValue:@"ios" forHTTPHeaderField:@"client"];
        _manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/json",@"text/plain",@"application/json",@"charset=utf-8",nil];
    
    }
    return self;
}

+ (AFHTTPRequestOperation *)doTaskWithURL:(NSString*)url
                               parameters:(NSDictionary*)model
                                   before:(void (^)(void))before
                                  success:(void (^)(BaseResponse *rsponse))success
                                  failure:(void (^)( NSError *error))failure
{
    if (url==nil||[url length]==0)
    {
        url=HTTP_DEFAULT_URL;
    }
    
    NSMutableDictionary *reqData=[[NSMutableDictionary alloc]initWithDictionary:model];
    
    NSData* jsonRequestData = [NSJSONSerialization dataWithJSONObject:reqData
                                                              options:kNilOptions
                                                                error:nil];
    NSString* jsonRequestString = [[NSString alloc] initWithData:jsonRequestData
                                                        encoding: NSUTF8StringEncoding];
    NSLog(@"requesturl=%@?data=%@",url,jsonRequestString);
    
    if (before)
    {
        before();
    }
    
    AFHTTPRequestOperationManager *manager = [[BaseHTTPManager sharedManager]manager];
    AFHTTPRequestOperation *operation=[manager POST:url
                                         parameters:@{
                                                      @"data":jsonRequestString
                                                      }
                                            success:^(AFHTTPRequestOperation *operation, id responseObject)
                                       {
                                           //NSLog(@"JSON: %@", responseObject);
                                           NSError* error = nil;
                                           BaseResponse *rsp= [[BaseResponse alloc] initWithDictionary:responseObject error:&error];
                                           if (error)
                                           {
                                               failure(error);
                                               return;
                                           }
                                           
                                           if (success)
                                           {
                                               success(rsp);
                                           }
                                       }
                                            failure:^(AFHTTPRequestOperation *operation, NSError *error)
                                       {
                                           NSLog(@"ERROR: %@", error);
                                           failure(error);
                                       }];
    
    return operation;
}

+ (AFHTTPRequestOperation *)doTaskWithURL:(NSString*)url
                               parameters:(NSDictionary*)model
                                  success:(void (^)(BaseResponse *rsp))success
                                  failure:(void (^)( NSError *error))failure
{
    return [[self class]doTaskWithURL:url parameters:model before:nil success:success failure:failure];
}

+ (AFHTTPRequestOperation *)doTaskWithURL:(NSString*)url
                               parameters:(NSDictionary*)model
                                 complete:(void (^)(NSError *error,BaseResponse* rsp))complete
{
    return [[self class]doTaskWithURL:url parameters:model before:nil complete:complete];
}

+ (AFHTTPRequestOperation *)doTaskWithURL:(NSString*)url
                               parameters:(NSDictionary*)model
                                   before:(void (^)(void))before
                                 complete:(void (^)(NSError *error, BaseResponse* rsp))complete
{
    if (url==nil||[url length]==0)
    {
        url=HTTP_DEFAULT_URL;
    }
    
    NSMutableDictionary *reqData=[[NSMutableDictionary alloc]initWithDictionary:model];
    
    NSData* jsonRequestData = [NSJSONSerialization dataWithJSONObject:reqData
                                                              options:kNilOptions
                                                                error:nil];
    NSString* jsonRequestString = [[NSString alloc] initWithData:jsonRequestData
                                                        encoding: NSUTF8StringEncoding];
    NSLog(@"requesturl=%@?data=%@",url,jsonRequestString);
    
    if (before)
    {
        before();
    }
    
    AFHTTPRequestOperationManager *manager = [[BaseHTTPManager sharedManager]manager];
    AFHTTPRequestOperation *operation=[manager POST:url
                                         parameters:@{
                                                      @"data":jsonRequestString
                                                      }
                                            success:^(AFHTTPRequestOperation *operation, id responseObject)
                                       {
                                           //NSLog(@"JSON: %@", responseObject);
                                           NSError* error = nil;
                                           BaseResponse *rsp= [[BaseResponse alloc] initWithDictionary:responseObject error:&error];
                                           if (complete)
                                           {
                                               complete(error,rsp);
                                           }
                                       }
                                            failure:^(AFHTTPRequestOperation *operation, NSError *error)
                                       {
                                           //NSLog(@"ERROR: %@", error);
                                           if (complete)
                                           {
                                               complete(error,nil);
                                           }
                                       }];
    
    return operation;
}

@end
